﻿/*
 * 版 本 v1.0
 * Copyright (c) 2013-2018 广州市同慧信息科技有限公司
 * 创建人：ftquan
 * 日 期：2017.03.22
 * 描 述：税率设置
 */
var selectedRow;
var refreshGirdData;
var grid;
var userdefault = {};
var bootstrap = function ($, learun) {
    "use strict";
    var companyId = '';

    var page = {
        init: function () {
            userdefault = learun.clientdata.get(['usersetting']);
            page.bind();
            page.initleft();
            page.initGrid_a();
            page.initGrid_b();
        },
        bind: function () {
            // 搜索框初始化
            $('#txt_Keyword').on("keypress", function (e) {
                if (event.keyCode == "13") {
                    var $this = $(this);
                    var keyword = $this.val();
                    page.search({keyword: keyword});
                }
            });
            // 公司选择
            $('#company_select').lrselect({
                type: 'tree',
                // placeholder: '请选择机构',
                height: 300,
                maxHeight: 300,
                // 是否允许搜索
                //allowSearch: true,
                select: function (item) {
                    if (item && item.value != '-1') {
                        companyId = item.value;
                        page.search();
                    }
                }
            });
            $('#company_select').lrselectRefresh({
                // 访问数据接口地址
                url: config.base_server() + 'companys/tree?access_token=' + config.getToken().access_token,
                // 访问数据接口参数
                param: {parentId: '0'}
            }).lrselectSet(userdefault.companyId);
            // 查询
            $('#btn_Search').on('click', function () {
                var keyword = $('#txt_Keyword').val();
                page.search({keyword: keyword});
            });
            // 刷新
            $('#lr_refresh').on('click', function () {
                location.reload();
            });
            //初始化默认款项
            $('#lr_default').on('click', function () {
                learun.layerConfirm('初始化默认数据会覆盖现有设置，是否按照默认税率初始化该账套?', function (res) {
                    if (res) {
                        learun.postForm(config.base_server() + 'gztaxs/init?companyId=' + companyId + '&access_token=' + config.getToken().access_token, {companyId: companyId}, function () {
                            refreshGirdData();
                        });
                    }
                });
            });
            // 新增
            $('#lr_add').on('click', function () {
                var data_value = $('#lr_left_list').find('.active').attr('data-value');
                selectedRow = null;
                learun.layerForm({
                    id: 'form',
                    title: '新增税率',
                    url: config.webportal_server + 'pages/salary/tax/form.html?companyId=' + companyId + '&access_token=' + config.getToken().access_token + '&category=' + data_value,
                    height: 420,
                    width: 500,
                    callBack: function (id) {
                        return top[id].acceptClick(refreshGirdData);
                    }
                });
            });
            // 编辑
            $('#lr_edit').on('click', function () {
                var data_value = $('#lr_left_list').find('.active').attr('data-value');
                var keyValue = "";
                if (data_value == '1') {
                    keyValue = $('#gridtable_a').jfGridValue('id');
                    selectedRow = $('#gridtable_a').jfGridGet('rowdata');
                } else if (data_value == '2') {
                    keyValue = $('#gridtable_b').jfGridValue('id');
                    selectedRow = $('#gridtable_b').jfGridGet('rowdata');
                }
                if (learun.checkrow(keyValue)) {
                    learun.layerForm({
                        id: 'form',
                        title: '编辑税率',
                        url: config.webportal_server + 'pages/salary/tax/form.html?companyId=' + companyId + '&access_token=' + config.getToken().access_token + '&category=' + data_value + '&keyValue=' + keyValue,
                        height: 420,
                        width: 500,
                        callBack: function (id) {
                            return top[id].acceptClick(refreshGirdData);
                        }
                    });
                }
            });
            // 删除
            $('#lr_delete').on('click', function () {
                var data_value = $('#lr_left_list').find('.active').attr('data-value');
                var keyValue = "";
                if (data_value == '1') {
                    keyValue = $('#gridtable_a').jfGridValue('id');
                } else if (data_value == '2') {
                    keyValue = $('#gridtable_b').jfGridValue('id');
                }
                if (learun.checkrow(keyValue)) {
                    learun.layerConfirm('该动作执行后此项将彻底被删除，是否确认删除该项?', function (res) {
                        if (res) {
                            learun.deleteForm(config.base_server() + 'gztaxs/delete/' + keyValue + '?access_token=' + config.getToken().access_token, {keyValue: keyValue}, function () {
                                refreshGirdData();
                            });
                        }
                    });
                }
            });
        },
        initleft: function () {
            $('#lr_left_list li').on('click', function () {
                var $this = $(this);
                var $parent = $this.parent();
                $parent.find('.active').removeClass('active');
                $this.addClass('active');

                var data_value = $this.attr('data-value');

                switch (data_value) {
                    case "1":
                        grid = $("#gridtable_dz");
                        $("#gridtable_a").show();
                        $("#gridtable_b").hide();

                        break;
                    case "2":
                        grid = $("#gridtable_b");
                        $("#gridtable_a").hide();
                        $("#gridtable_b").show();

                        break;
                    default:
                        grid = $("#gridtable_a");
                        $("#gridtable_a").show();
                        $("#gridtable_b").hide();

                        break;
                }

                page.search();
            });
        },
        initGrid_a: function () {
            $('#gridtable_a').jfGrid({
                url: config.base_server() + 'gztaxs/list?category=1&access_token=' + config.getToken().access_token,
                headData: [
                    {label: '描述', name: 'description', width: 250, align: 'left'},
                    {label: '税费代码', name: 'taxCode', width: 100, align: 'left'},
                    {
                        label: "含税收入税距", name: "含税收入税距", width: 300, align: 'center',
                        children: [
                            {
                                label: "下限", name: "taxInclusiveStart", width: 300, align: "right",
                                formatter: function (cellvalue) {
                                    return $.formatMoney(cellvalue, 2)
                                }
                            },
                            {
                                label: "上限", name: "taxInclusiveEnd", width: 150, align: "right",
                                formatter: function (cellvalue) {
                                    return $.formatMoney(cellvalue, 2)
                                }
                            },
                        ]
                    },
                    {
                        label: "不含税收入税距", name: "不含税收入税距", width: 300, align: 'center',
                        children: [
                            {
                                label: "下限", name: "taxExclusiveStart", width: 150, align: "right",
                                formatter: function (cellvalue) {
                                    return $.formatMoney(cellvalue, 2)
                                }
                            },
                            {
                                label: "上限", name: "taxExclusiveEnd", width: 150, align: "right",
                                formatter: function (cellvalue) {
                                    return $.formatMoney(cellvalue, 2)
                                }
                            },
                        ]
                    },
                    {
                        label: '税率', name: 'rate', width: 80, align: 'right',
                        formatter: function (cellvalue) {
                            return cellvalue + "%";
                        }
                    },
                    {
                        label: '扣除数', name: 'money', width: 180, align: 'right',
                        formatter: function (cellvalue) {
                            return $.formatMoney(cellvalue, 2)
                        }
                    },
                    {
                        label: "更新时间", name: "updateTime", width: 140, align: "center",
                        formatter: function (value) {
                            return learun.formatDate(value, 'yyyy-MM-dd hh:mm:ss');
                        }
                    }
                ],
                dblclick: function (row) {
                    var data_value = $('#lr_left_list').find('.active').attr('data-value');
                    var keyValue = $('#gridtable_a').jfGridValue('id');
                    selectedRow = row;
                    if (learun.checkrow(keyValue)) {
                        learun.layerForm({
                            id: 'form',
                            title: '编辑税率',
                            url: config.webportal_server + 'pages/salary/tax/form.html?access_token=' + config.getToken().access_token + '&category=' + data_value + '&keyValue=' + keyValue,
                            height: 420,
                            width: 500,
                            callBack: function (id) {
                                return top[id].acceptClick(refreshGirdData);
                            }
                        });
                    }
                },
                isPage: true,
                reloadSelected: true,
                mainId: 'id'
            });
        },
        initGrid_b: function () {
            $('#gridtable_b').jfGrid({
                url: config.base_server() + 'gztaxs/list?category=2&access_token=' + config.getToken().access_token,
                headData: [
                    {label: '描述', name: 'description', width: 250, align: 'left'},
                    {label: '税费代码', name: 'taxCode', width: 100, align: 'left'},
                    {
                        label: "含税收入税距", name: "含税收入税距", width: 300, align: 'center',
                        children: [
                            {
                                label: "下限", name: "taxInclusiveStart", width: 150, align: "right",
                                formatter: function (cellvalue) {
                                    return $.formatMoney(cellvalue, 2)
                                }
                            },
                            {
                                label: "上限", name: "taxInclusiveEnd", width: 150, align: "right",
                                formatter: function (cellvalue) {
                                    return $.formatMoney(cellvalue, 2)
                                }
                            },
                        ]
                    },
                    {
                        label: "不含税收入税距", name: "不含税收入税距", width: 300, align: 'center',
                        children: [
                            {
                                label: "下限", name: "taxExclusiveStart", width: 150, align: "right",
                                formatter: function (cellvalue) {
                                    return $.formatMoney(cellvalue, 2)
                                }
                            },
                            {
                                label: "上限", name: "taxExclusiveEnd", width: 150, align: "right",
                                formatter: function (cellvalue) {
                                    return $.formatMoney(cellvalue, 2)
                                }
                            },
                        ]
                    },
                    {
                        label: '税率', name: 'rate', width: 80, align: 'right',
                        formatter: function (cellvalue) {
                            return cellvalue + "%";
                        }
                    },
                    {
                        label: '扣除数', name: 'money', width: 180, align: 'right',
                        formatter: function (cellvalue) {
                            return $.formatMoney(cellvalue, 2)
                        }
                    },
                    {
                        label: "更新时间", name: "updateTime", width: 140, align: "center",
                        formatter: function (value) {
                            return learun.formatDate(value, 'yyyy-MM-dd hh:mm:ss');
                        }
                    }
                ],
                dblclick: function (row) {
                    var data_value = $('#lr_left_list').find('.active').attr('data-value');
                    var keyValue = $('#gridtable_b').jfGridValue('id');
                    selectedRow = row;
                    if (learun.checkrow(keyValue)) {
                        learun.layerForm({
                            id: 'form',
                            title: '编辑税率',
                            url: config.webportal_server + 'pages/salary/tax/form.html?access_token=' + config.getToken().access_token + '&category=' + data_value + '&keyValue=' + keyValue,
                            height: 420,
                            width: 500,
                            callBack: function (id) {
                                return top[id].acceptClick(refreshGirdData);
                            }
                        });
                    }
                },
                isPage: true,
                reloadSelected: true,
                mainId: 'id'
            });
        },
        search: function (param) {
            param = param || {};
            param.companyId = companyId;
            param.keyword = $("#txt_Keyword").val();

            var data_value = $('#lr_left_list').find('.active').attr('data-value');
            switch (data_value) {
                case "1":
                    $('#gridtable_a').jfGridSet('reload', param);
                    break;
                case "2":
                    $('#gridtable_b').jfGridSet('reload', param);
                    break;
            }
        }
    };

    refreshGirdData = function () {
        page.search();
    };

    page.init();
}


